<?php

namespace App\Http\Controllers;

use App\AccessTokens;
use App\Articles;
use App\Members;
use Illuminate\Http\Request;

class MembersController extends Controller
{
    public function login(Request $request){
        $data = $request->all();
        $res = $this->checkSign($data['sign']);
        if(!$res['status']){
            return json_encode(['status'=>0,'msg'=>$res['msg']]);
        }
        $isHas = Members::where('u_openid',$data['u_openid'])->first();
        if($isHas){
            return json_encode(['status'=>'ok','data'=>$isHas]);
        }

        //不知道前端哪个里多出来的，可能是框架带的
        if(isset($data['s'])){
            unset($data['s']);
        }
        $data['u_random'] = uniqid();
        $data['u_regtime']= time();
        $data['u_id'] = Members::insertGetId($data);
        return json_encode(['status'=>'ok','data'=>$data]);
    }

    public function codeToSession(Request $request){
        $appid = 'wxdcbbf1cdbcef19d2';
        $secrect = 'ac9611b7e55790a2b9d967571e997be2';
        $code = $request->post('code');
        $url =  "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid.
            "&secret=".$secrect."&js_code=".$code."&grant_type=authorization_code";
        $res = $this->cUrlRequest($url);
        return json_encode(['data'=>$res]);
    }

    /**
     * 获取 微信的 UnionID
     */
    public function wxaes(Request $request){
        $data = $request->all();

        if(empty($data['session_key']) || empty($data['encryptedData']) || empty($data['iv'])){
            return json_encode(['status'=>0,'msg'=>'参数有缺']);
        }
        $pc = new WXBizDataCrypt('wxdcbbf1cdbcef19d2', $data['session_key']);
        $res = '';
        $errCode = $pc->decryptData($data['encryptedData'], $data['iv'], $res);
        if ($errCode == 0) {
            return json_encode(['status'=>'ok','msg'=>$res]);
        } else {
            return json_encode(['status'=>0,'msg'=>$errCode]);
        }
    }

    public function getAccessToken(){
        $data['token'] = uniqid();
        $data['time'] = time();
        AccessTokens::insert($data);
        return json_encode(['status'=>'ok','data'=>$data]);
    }

    public function info(Request $request){
        $data = $request->all();
        $user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }
        // 查询会员文章总数
        $artCountNumber = Articles::where('art_uid',$data['uid'])->count();
        $user['artCount'] = $artCountNumber;
        return json_encode(['status'=>'ok','data'=>$user]);
    }

    protected function cUrlRequest($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


}
